﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LOGIN
{
    public partial class CompanyTable : Form
    {
        public CompanyTable()
        {
            InitializeComponent();
            this.dgv_company.AllowUserToAddRows = false;
            this.dgv_CompanyDetail.AllowUserToAddRows = false;
        }

        private void CompanyTable_Load(object sender, EventArgs e)
        {
            
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT SupplyID,Supplyname FROM dbo.supplyer ;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable CompanyTable = new DataTable();

            sqlConnection.Open();

            sqlDataAdapter.Fill(CompanyTable);

            sqlConnection.Close();
            this.dgv_company.DataSource = CompanyTable;
            dgv_company.Columns["SupplyID"].HeaderText = "序号";
            dgv_company.Columns["Supplyname"].HeaderText = "供应商";
            this.dgv_company.RowHeadersVisible = false;
            dgv_company.ReadOnly = true;
            
        }

       
    
        private void dgv_company_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_company.SelectedCells.Count != 0)
            {
                int index = dgv_company.CurrentRow.Index;
                ordersHelper.companyID= dgv_company.Rows[index].Cells[0].Value.ToString();
                
            }
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT c.SupplyID,Supplyname,OrderKinds,OrderDate,GetDate,ProducePla,IndPrice,Num,Guige FROM dbo.orders cs JOIN dbo.supplyer c ON cs.SupplyID = c.SupplyID WHERE cancel=0 AND State=1 AND c.SupplyID= @supplyID;";
            sqlCommand.Parameters.AddWithValue("@supplyID", ordersHelper.companyID.Trim());
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable CompanyTable = new DataTable();

            sqlConnection.Open();

            sqlDataAdapter.Fill(CompanyTable);

            sqlConnection.Close();
            this.dgv_CompanyDetail.DataSource = CompanyTable;
            dgv_CompanyDetail.Columns["SupplyID"].HeaderText = "序号";
            dgv_CompanyDetail.Columns["Supplyname"].HeaderText = "供应商";
            dgv_CompanyDetail.Columns["OrderKinds"].HeaderText = "订单";
            dgv_CompanyDetail.Columns["OrderDate"].HeaderText = "订单时间";
            dgv_CompanyDetail.Columns["GetDate"].HeaderText = "到货时间";
            dgv_CompanyDetail.Columns["ProducePla"].HeaderText = "生产地";
            dgv_CompanyDetail.Columns["IndPrice"].HeaderText = "入库价格";
            dgv_CompanyDetail.Columns["Num"].HeaderText = "数量";
            dgv_CompanyDetail.Columns["Guige"].HeaderText = "规格";
            this.dgv_CompanyDetail.RowHeadersVisible = false;
            dgv_CompanyDetail.ReadOnly = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string fileName = "";

            string saveFileName = "";

            SaveFileDialog saveDialog = new SaveFileDialog();

            saveDialog.DefaultExt = "xls";

            saveDialog.Filter = "Excel文件|*.xls";

            saveDialog.FileName = fileName;

            saveDialog.ShowDialog();

            saveFileName = saveDialog.FileName;

            if (saveFileName.IndexOf(":") < 0) return; 

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {

                MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");

                return;

            }

            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;

            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];



            for (int i = 0; i < dgv_CompanyDetail.ColumnCount; i++)

            { worksheet.Cells[1, i + 1] = dgv_CompanyDetail.Columns[i].HeaderText; }



            for (int r = 0; r < dgv_CompanyDetail.Rows.Count; r++)
            {
                for (int i = 0; i < dgv_CompanyDetail.ColumnCount; i++)
                {

                    worksheet.Cells[r + 2, i + 1] = dgv_CompanyDetail.Rows[r].Cells[i].Value;

                }

                System.Windows.Forms.Application.DoEvents();

            }

            worksheet.Columns.EntireColumn.AutoFit();

            MessageBox.Show(fileName + "信息导出成功", "提示", MessageBoxButtons.OK);

            if (saveFileName != "")
            {

                try
                {
                    workbook.Saved = true;

                    workbook.SaveCopyAs(saveFileName);

                }

                catch (Exception ex)
                {

                    MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);

                }

            }

            xlApp.Quit();

            GC.Collect();
        }

       
        private void button1_Click(object sender, EventArgs e)
        {
            CompanyTable f = new CompanyTable();
            f.Close();

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

    }
}
